﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Print_UI
{
    public partial class FrmStatistcStudent : CommonForm
    {
        public FrmStatistcStudent()
        {
            InitializeComponent();
        }

        private void FrmStatistcStudent_Load(object sender, EventArgs e)
        {
            LoadDepartment();
            LoadData();
        }
        private void LoadDepartment()
        {
            try
            {
                var departmentAdmin = from obj in Model.CAT_Department.OrderBy(c => c.Name)
                                      select obj;
                Dictionary<int, string> dicAdmin = new Dictionary<int, string>();
                dicAdmin.Add(-1, "----Chọn Khoa----");
                foreach (var item in departmentAdmin)
                {
                    dicAdmin.Add(item.ID, item.Name);

                }
                cboDepartment.Properties.ValueMember = "Key";
                cboDepartment.Properties.DisplayMember = "Value";
                cboDepartment.Properties.DataSource = dicAdmin;
                cboDepartment.EditValue = -1;

            }
            catch (Exception ex) { MessageBox.Show(ex.Message); }
        }
        private void LoadData()
        {
            int iDepartment = Convert.ToInt32(cboDepartment.EditValue);
            var query =from obj in Model.PRI_Student.Where(c => c.DepartmentID == iDepartment)
                       select new { obj .Code,obj.ID,obj.Firstname,obj.Lastname,obj.YearID,obj.CAT_Class.Name,sectorname = obj.CAT_Sector.Name};

            gvdata.DataSource = query;
        }

        private void cboDepartment_EditValueChanged(object sender, EventArgs e)
        {
            LoadData();
        }

        private void cboYear_EditValueChanged(object sender, EventArgs e)
        {
            LoadData();
        }

        private void gridView1_DoubleClick(object sender, EventArgs e)
        {
            try
            {
                DevExpress.XtraGrid.Views.Grid.GridView view = (DevExpress.XtraGrid.Views.Grid.GridView)sender;
                Point pt = view.GridControl.PointToClient(Control.MousePosition);
                DevExpress.XtraGrid.Views.Grid.ViewInfo.GridHitInfo info = view.CalcHitInfo(pt);
                if (info.InRow || info.InRowCell)
                {
                    int iStudentID = Convert.ToInt32(gridView1.GetFocusedRowCellValue("ID"));
                    FrmClassOfStudent frm = new FrmClassOfStudent();
                    frm.iStudentID = iStudentID;
                    frm.ShowDialog();
                    if (frm.DialogResult == DialogResult.OK)
                    {
                        LoadData();
                    }
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnCancle_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (MessageBox.Show("Thoát khỏi giao diện này", "Thông Báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
            {
                this.Close();

            }
        }

        private void btnRefresh_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            RefreshModel();
            LoadData();
        }
    }
}
